<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\ChargebackModel;
use app\model\DishesModel;
use app\model\UserModel;
use app\model\StaffModel;
use think\facade\Request;
use think\facade\Validate;

class Chargeback
{
    //获取待处理退款信息表
    public function list()
    {

        $params = Request::param();
        if($params['chargeback_status'] == 0){
            $params['chargeback_status'] = 1;
        }else if($params['chargeback_status'] == 1){
            $params['chargeback_status'] = 2;
        }
        $validate = Validate::rule([
            'chargeback_status|退款订单状态' => 'require'
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }
        $chargebackList = ChargebackModel::where('chargeback_status','=',$params['chargeback_status'])->select();
        foreach ($chargebackList as &$value){
            //用户昵称
            $userOne = UserModel::where('open_id','=',$value['open_id'])->find();
            $value['nickname'] = $userOne['nickname'];
            //操作员
            $staffOne = StaffModel::find($value['staff_id']);
            $value['username'] = $staffOne['username'];
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'chargebackList' => $chargebackList,
                'chargeback_status' => $params['chargeback_status']
            ],
        ];
        return json($data);
    }

    //菜品删除
    public function delete()
    {
        $dishesId = Request::param("dishes_id");
        $validate = Validate::rule([
            'dishes_id|菜品id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['dishes_id' => $dishesId])) {
            echo $validate->getError();
            exit();
        }
        $category=DishesModel::find($dishesId);
        $result = DishesModel::destroy($category);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //修改退款订单信息
    public function updtchargeback()
    {
        // 1. 获取点赞id
        // 2. 修改点赞状态status = 2就可以了。

        $params = Request::param();
        $validate = Validate::rule([
            'chargeback_id|退款订单号' => 'require',
            'staff_id|员工id'=>'require|between:1,' . PHP_INT_MAX,
//            'article_id|文章id' => 'require',
//            'like_status|点赞状态' => 'require|between:1,' . PHP_INT_MAX,

        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }


        $chargeback = ChargebackModel::where("chargeback_id","=",$params['chargeback_id'])
//            ->where("article_id","=",$likeList['article_id'])
            ->find();
            if ($chargeback['chargeback_status']==1){
                $chargeback['chargeback_status']=2;
            }else{
                $chargeback['chargeback_status']=1;
            }
        $chargeback['staff_id'] = $params['staff_id'];
//        var_dump($like['like_status']);
        $chargeback['update_time']=time();
//        $likelist=$like->toArray();
            $result = $chargeback->save();

//        var_dump($like->toArray());

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ],
        ];
        return json($data);
    }
}
